<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\UserBingshi;
use App\Admin\Repositories\Config;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Admin;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use App\Models\User;
class UserBingshiController extends AdminController
{
    protected $translation = 'user-bingshi';
    /**
     * Get translation path.
     *
     * @return string
     */
    public function setTranslation($translation)
    {
        $this->translation = $translation;
        
        return $this;
    }
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new UserBingshi('user'), function (Grid $grid) {
            $grid->model()->orderBy('id', 'desc');
            if(!\Dcat\Admin\Admin::user()->isAdministrator()) {
                $grid->model()->where('user.shop_id', session('shop_id'));
            }
            $grid->column('id')->sortable();
            $grid->column('user.name', '姓名');
            $grid->column('zhusu');
            // $grid->column('daijing');
            // $grid->column('xiguan');
            // $grid->column('kuangjia_date');
            // $grid->column('quguang_dpt');
            // $grid->column('xianyou_date');
            // $grid->column('jiaomo_date');
            // $grid->column('tingdai_date');
            // $grid->column('other');
            // $grid->column('jiwangshi');
            // $grid->column('jiwangshi_other');
            // $grid->column('fuqin');
            // $grid->column('muqin');
            // $grid->column('qinqi');
            // $grid->column('jiazu');
            // $grid->column('chusheng');
            $grid->column('created_at');
            // $grid->column('updated_at')->sortable();
        
            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand();
                $filter->like('user.name', '姓名')->width(3);
                $filter->like('user.mobile', '手机号')->width(3);
        
            });
        });
    }
    

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new UserBingshi(), function (Show $show) {
            $model = $show->model();
            $show->html(view('admin.show.bingshi', compact('model')));
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    public function form($id = null)
    {
        return Form::make(new UserBingshi(), function (Form $form) use ($id) {
            if($id == -1) {
                $form->disableFooter();
                $form->disableHeader();
            }else{
                $form->select('user_id', '用户')->options(function ($id) {
                    $user = User::find($id);
                    if ($user) {
                        return [$user->id => $user->name];
                    }
                })->ajax(admin_url('api/user'))->required()->setFieldClass('col-md-8', true);
            }
            // $form->display('id');
                
                $form->textarea('zhusu')->setFieldClass('col-md-8', true)->required();
                $form->radio('daijing')->options(['1' => '是', '0' => '否'])->default(0)->when([1], function (Form $form) {
                    $form->radio('xiguan')->options(['1' => '常带', '0' => '不常带']);
                    $form->date('kuangjia_date')->format('YYYY-MM');
                    $form->date('xianyou_date')->format('YYYY-MM');
                    $form->quguangdushu('quguang_dpt');
                    $form->date('jiaomo_date')->format('YYYY-MM');
                    $form->date('tingdai_date')->format('YYYY-MM');
                })->required();;
                
                
                
            
                
            
                $form->textarea('other')->required();;
           
                $form->checkbox('jiwangshi')->options(Config::selectOptions(399))->required();;
           
                $form->textarea('jiwangshi_other', '');
            
                $form->checkbox('fuqin')->options(Config::selectOptions(pid: 407))->required();;
                // $form->checkbox('qinqi')->options(Config::selectOptions(pid: 407))->setLabelClass('col-md-4', true);
            
           
                $form->checkbox('muqin')->options(Config::selectOptions(pid: 407))->required();;
            
                $form->text('jiazu');
                $form->checkbox('chusheng')->options(Config::selectOptions(pid: 412))->required();;
            
            
            
        });
    }
}
